package com.alan.selenium;

import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.*;
import java.util.Date;

/**
 * @author: Alan
 * @date: 2020/9/14 21:55
 */
public class ChromeTest {

    public static void main(String[] args) throws InterruptedException {
        System.setProperty("webdriver.chrome.driver", "D:\\IT_Program\\apache-jmeter-5.3\\jar\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://www.baidu.com");
        Thread.sleep(1000);
        File file = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        try {
            FileInputStream fis = new FileInputStream(file);
            FileOutputStream fos = new FileOutputStream("D:\\temp\\img\\"+ new Date().getTime()+".png");
            int ch;
            //这样写可以防止最后取值到-1的情况
            while((ch=fis.read()) != -1){
                fos.write(ch);
            }
            fos.flush();
            fis.close();
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        driver.quit();
    }

}
